<?php

class Pdetail extends Controller {
	
    function index($index) {
        global $imgedit;
	
        $this->set('title','YOU - Photo');
        $result = $this->PdetailModel->query('SELECT * FROM foto WHERE idfoto=\''.mysql_real_escape_string($index).'\'');
        $result1 = $this->PdetailModel->query('SELECT * FROM key_foto WHERE idfoto=\''.mysql_real_escape_string($index).'\'');
        $profpic = $result[0]['Foto']['pic'];
        $deskripsi = $result[0]['Foto']['deskripsi'];
        $p = $result[0]['Foto']['pic'];
        $p1 = $result[0]['Foto']['idkategori'];
        $p2 = $result[0]['Foto']['deskripsi'];
        $p3 = $result[0]['Foto']['tgl_foto'];
        $p4 = $result[0]['Foto']['tgl_upload'];
        $p5 = $result[0]['Foto']['hak_akses'];
        $p6 = $result[0]['Foto']['hak_komen'];
        $p7 = $result1[0]['Key_foto']['idkey'];
        $imgedit->load($profpic);
        $imgedit->scale(50);
        $img = BASE_PATH.'/'.$this->PdetailModel->getFilename($profpic).'_thumb'.$this->PdetailModel->getExtension($profpic);
        $imgedit->save($this->PdetailModel->getFilename($profpic).'_thumb'.$this->PdetailModel->getExtension($profpic));
        $this->set('indexFoto',$index);
        $this->set('pimg',$img);
        $this->set('des',$deskripsi);
        $this->set('p',$p);
        $this->set('p1',$p1);
        $this->set('p2',$p2);
        $this->set('p3',$p3);
        $this->set('p4',$p4);
        $this->set('p5',$p5);
        $this->set('p6',$p6);
        $this->set('p7',$p7);
    }
	
    function delete($index) {
        $this->PdetailModel->query('DELETE FROM foto WHERE idfoto =\''.mysql_real_escape_string($index).'\'');
        $this->_auto_render = false;
        $this->redirect(BASE_PATH . "photo/index");
    }

    function postkomen($index) {
        global $session;

        $user_id = $session->get('userid');
        $field = array();
        $field['idfoto'] = $index;
        $field['iduser'] = $user_id;
        $field['isi'] = $_POST['post'];
        $this->PdetailModel->insertkomen($field);
        
        $this->_auto_render = false;
        $this->redirect(BASE_PATH.'pdetail/index/'.$index);
    }
	
/*	function register(){
		$field = array();
		$field['fullname'] = $_POST['fnama'];
		$field['username'] = $_POST['fuser'];
		$field['password'] = $_POST['fpass'];
		$field['tgllahir'] = $_POST['ftanggal'];
		$field['email'] = $_POST['femail'];
		$field['kota'] = $_POST['flokasi'];
		$field['gender'] = $_POST['fgender'];
		$field['pp'] = $_FILES['fprofpict'];
                $result = $this->HomeModel->query('select username from user where Username=\''.mysql_real_escape_string($field['username']).'\'');
		if(count($result) != 0){
//			$this->set('title','Failed');
			$this->set('field',null);
		} else {
			$this->HomeModel->insert($field);
                        //global $session;
                        //$session->set('userid',$result[0]['User']['idUser']);
                        //$this->set('title',$result[0]['User']['fullname']);
			$this->set('field',$field);
		}
	}
	
	function login(){

                //jangan lupa kalo mau make variabel $session harus definisiin "global $session" soalnya dia static
                global $session;

                $field = array();
                $field['username'] = $_POST['luser'];
                $field['password'] = $_POST['lpass'];

                $result = $this->HomeModel->query('select * from user where Username=\''.mysql_real_escape_string($field['username']).'\' and password=\''.mysql_real_escape_string($field['password']).'\'');
                //$res = mysql_fetch_array($result[0]);
                //print_r(count($result));
                if(count($result) != 0){
                        //success login
                        //print_r("Login berhasil");
                        $this->redirect('http://localhost/YOU/home/index');
                        //$this->set('title',$result[0]['User']['fullname']);
                        //ini ngeset variabel session dengan nama userid sesuai dengan value di parameter kedua
                        //$session->set('userid',$result[0]['User']['idUser']);
                        //ini buat dapetin value yang disimpan di session dengan nama userid
                        //$session->get('userid');
                        //fungsi lainnya lo liat di folder application/session.classs.php intinya kalo mau make harus deklarasiin global $session dulu di fungsi
                        //print_r($session);
                }else{
                        //failed login
                        $this->_auto_render = false;
                        //$this->set('title','Failed');
                        $this->redirect('http://localhost/YOU/home/index');
                }
                $this->_auto_render = false;
//                $this->redirect('http://localhost/home/index');
//                $this->redirect('index');
	}*/
	
	function beforeAction(){
		$this->set('title',null);
	}
	

}
